package org.gwt.mosaic.samples.addressbook.client.ui;

import java.util.ArrayList;

import org.gwt.beansbinding.core.client.BeanProperty;
import org.gwt.beansbinding.core.client.Binding;
import org.gwt.beansbinding.core.client.BindingGroup;
import org.gwt.beansbinding.core.client.Bindings;
import org.gwt.beansbinding.core.client.AutoBinding.UpdateStrategy;
import org.gwt.beansbinding.observablecollections.client.ObservableCollections;
import org.gwt.beansbinding.observablecollections.client.ObservableList;
import org.gwt.mosaic.beansbinding.client.ListBoxBinding;
import org.gwt.mosaic.beansbinding.client.MosaicBindings;
import org.gwt.mosaic.samples.addressbook.client.model.Person;
import org.gwt.mosaic.samples.addressbook.client.util.DataGenerator;
import org.gwt.mosaic.ui.client.LayoutComposite;
import org.gwt.mosaic.ui.client.ListBox;
import org.gwt.mosaic.ui.client.ListBox.CellRenderer;
import org.gwt.mosaic.ui.client.layout.BorderLayout;
import org.gwt.mosaic.ui.client.layout.BorderLayoutData;
import org.gwt.mosaic.ui.client.layout.LayoutPanel;
import org.gwt.mosaic.ui.client.layout.BorderLayout.Region;

public class ContactList extends LayoutComposite {

  private final ListBox<Person> contactList = new ListBox<Person>(new String[] {
      "First Name", "Last Name", "Email"});

  private final ContactForm contactForm = new ContactForm();

  private final ObservableList<Person> list;

  @SuppressWarnings("unchecked")
  public ContactList() {
    super(new BorderLayout());

    final LayoutPanel layoutPanel = getLayoutPanel();
    layoutPanel.setPadding(0);

    contactList.setCellRenderer(new CellRenderer<Person>() {
      public void renderCell(ListBox<Person> listBox, int row, int column,
          Person item) {
        switch (column) {
          case 0:
            listBox.setText(row, column, item.getFirstName());
            break;
          case 1:
            listBox.setText(row, column, item.getLastName());
            break;
          case 2:
            listBox.setText(row, column, item.getEmail());
            break;
        }
      }
    });

    list = ObservableCollections.observableList(new ArrayList<Person>());

    // create the binding from List to ListBox
    final ListBoxBinding listBoxBinding = MosaicBindings.createListBoxBinding(
        UpdateStrategy.READ, list, contactList);

    // add columns bindings to the ListBoxBinding
    listBoxBinding.addColumnBinding(BeanProperty.create("firstName"));
    listBoxBinding.addColumnBinding(BeanProperty.create("lastName"));
    listBoxBinding.addColumnBinding(BeanProperty.create("email"));

    final Binding textBoxBinding1 = Bindings.createAutoBinding(
        UpdateStrategy.READ_WRITE, contactList,
        BeanProperty.create("selectedElement.firstName"),
        contactForm.getFirstNameInput(), BeanProperty.create("text"));

    final Binding textBoxBinding2 = Bindings.createAutoBinding(
        UpdateStrategy.READ_WRITE, contactList,
        BeanProperty.create("selectedElement.lastName"),
        contactForm.getLastNameInput(), BeanProperty.create("text"));

    final Binding textBoxBinding3 = Bindings.createAutoBinding(
        UpdateStrategy.READ_WRITE, contactList,
        BeanProperty.create("selectedElement.email"),
        contactForm.getEmailInput(), BeanProperty.create("text"));

    // realize the binding
    final BindingGroup bindingGroup = new BindingGroup();
    bindingGroup.addBinding(listBoxBinding);
    bindingGroup.addBinding(textBoxBinding1);
    bindingGroup.addBinding(textBoxBinding2);
    bindingGroup.addBinding(textBoxBinding3);
    bindingGroup.bind();

    layoutPanel.add(contactList);
    layoutPanel.add(contactForm, new BorderLayoutData(Region.SOUTH, 50, 350,
        true));

    // generate random data
    list.addAll(new DataGenerator().generatePersons(100));

    addStyleName("ContactList");
  }

}
